package com.mall.b2c.service.impl;


import com.mall.b2c.service.StaticService;
import com.mall.common.dao.ArticleDao;
import com.mall.common.domain.content.ArticleDomain;
import com.mall.common.domain.content.TemplateDomain;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.TemplateService;
import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=true)
public class StaticServiceImpl implements StaticService, ServletContextAware {

//	private static final Integer staticNum = Integer.valueOf(40000);
	private ServletContext servletContext;
	@Resource private TemplateService templateService;

	@Resource private ArticleDao articleDao;
	@Resource private FreeMarkerConfigurer freemarkerConfigurer;

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public int build(String templatePath, String staticPath, String outPath, Map<String, Object> model) {
		Assert.hasText(templatePath);
		Assert.hasText(staticPath);
		Template template = null;
		try {
			//读取模板
			template = freemarkerConfigurer.getConfiguration().getTemplate(templatePath, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}


		File file = new File(outPath);
		File file1 = file.getParentFile();
		if (!file1.exists()){
			file1.mkdirs();
		}
		FileOutputStream fileoutputstream = null;
		OutputStreamWriter outputstreamwriter = null;
		BufferedWriter bufferedwriter = null;
		try {
			fileoutputstream = new FileOutputStream(file);
			outputstreamwriter = new OutputStreamWriter(fileoutputstream, "UTF-8");
			bufferedwriter = new BufferedWriter(outputstreamwriter);
			template.process(model, bufferedwriter);//生成静态化文件
			bufferedwriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(bufferedwriter);
			IOUtils.closeQuietly(outputstreamwriter);
			IOUtils.closeQuietly(fileoutputstream);
		}

		return 1;
	}

	public int build(ArticleDomain article) {
		Assert.notNull(article);
		delete(article);
		String templatePath = templateService.getTemplatePathByName("articleContent");
		int i = 0;
		if (article.getIsPublication()) {
			HashMap<String, Object> hashmap = new HashMap<String, Object>();
			hashmap.put("article", article);
			i += build(templatePath, article.getPath(),article.getAboutPath(), hashmap);
		}
		return i;
	}

	
	/*public int buildAll() {
		int i = 0;
		for (int j = 0; (long)j < articleDao.count(); j += 20) {
			List<ArticleDomain> list = articleDao.findList(j, 20);
			for (Iterator<ArticleDomain> iterator = list.iterator(); iterator.hasNext();) {
				ArticleDomain article = iterator.next();
				i += build(article);
			}
			articleDao.clear();
		}
		return i;
	}*/

	public int delete(String staticPath) {
		Assert.hasText(staticPath);
		File file = new File(servletContext.getRealPath(staticPath));
		if (file.exists()) {
			file.delete();
			return 1;
		} else {
			return 0;
		}
	}

	public int delete(ArticleDomain article) {
		Assert.notNull(article);
		int k = delete(article.getAboutPath());
		return k;
	}
}
